/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.base.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.base.dao.StoreSelfProductDao;
import com.chlitina.store.modules.base.entity.StoreSelfProduct;
import com.chlitina.store.modules.base.entity.StoreSelfProductCategory;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 店面自营产品Service
 * @author liujiang
 * @version 2013-11-03
 */
@Component
@Transactional(readOnly = true)
public class StoreSelfProductService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(StoreSelfProductService.class);
	
	@Autowired
	private StoreSelfProductDao storeSelfProductDao;
	
	public StoreSelfProduct get(Long id) {
		return storeSelfProductDao.findOne(id);
	}
	
	public Page<StoreSelfProduct> find(Page<StoreSelfProduct> page, String name, Long category_id, Integer status) {
		Store store = StoreUtils.getStore();
		DetachedCriteria dc = storeSelfProductDao.createDetachedCriteria();
		if (StringUtils.isNotEmpty(name)){
			dc.add(Restrictions.like("name", "%"+ name +"%"));
		}
		if (category_id != null){
			dc.add(Restrictions.eq("storeSelfProductCategory.id", category_id));
		}
		if(status != null) {
			dc.add(Restrictions.eq("status", status));
		} else {
			dc.add(Restrictions.eq("status", StoreSelfProduct.FLAG_ENABLE));
		}
		dc.add(Restrictions.like("store", store));
		dc.addOrder(Order.asc("id"));
		return storeSelfProductDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(StoreSelfProduct storeSelfProduct) {
		storeSelfProductDao.save(storeSelfProduct);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		storeSelfProductDao.delete(id);
	}
	public List<StoreSelfProduct> findByStore() {
		DetachedCriteria dc = storeSelfProductDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store", StoreUtils.getStore()));
		dc.addOrder(Order.asc("id"));
		return storeSelfProductDao.find( dc);
	}
	@Transactional(readOnly = false)
	public void changeStatus(StoreSelfProduct storeSelfProduct) {
		int status = storeSelfProduct.getStatus();
		status = status == StoreSelfProduct.FLAG_ENABLE ?  StoreSelfProduct.FLAG_DISABLE :  StoreSelfProduct.FLAG_ENABLE;
		storeSelfProductDao.changeStatus(status, storeSelfProduct.getId());
	}
	
	//获取当前记录排第几位
	public int getIndexById(Long currentId, StoreSelfProductCategory parent, Integer status) {
		Store store = StoreUtils.getStore();
		DetachedCriteria dc = storeSelfProductDao.createDetachedCriteria();
		dc.add(Restrictions.le("id", currentId));
		if(parent !=null) {
			dc.add(Restrictions.eq("storeSelfProductCategory", parent));
		}
		if(status !=null) {
			dc.add(Restrictions.eq("status", status));
		}
		dc.add(Restrictions.like("store", store));
		dc.addOrder(Order.asc("id"));
		return (int)storeSelfProductDao.count(dc);
	}
}
